package cn.est.controller;
import cn.est.pojo.Parets;
import cn.est.constants.Constants;
import cn.est.dto.Page;
import cn.est.dto.Result;
import cn.est.service.ParetsService;
import cn.est.utils.EmptyUtils;
import cn.est.utils.ResultUtils;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;

@Controller
@RequestMapping("/client/parets")
public class ParetsController {

    @Resource
    private ParetsService paretsService;

    @RequestMapping("/search")
    @ResponseBody
    public Page<Parets> searchParets(@RequestParam Map<String,Object> param) {
        Page<Parets> paretsPage = null;
        try {
            Integer pageNo= EmptyUtils.isEmpty(param.get("pageNo"))? Page.DEFAULT_PAGE_NO:Integer.parseInt((String) param.get("pageNo"));
            Integer pageSize= EmptyUtils.isEmpty(param.get("pageSize"))? Page.DEFAULT_PAGE_SIZE:Integer.parseInt((String) param.get("pageSize"));
            paretsPage  = paretsService.queryParetsPageByMap(param, pageNo, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return paretsPage;
    }

    @RequestMapping("/saveOrUpdateParets")
    @ResponseBody
    public Result saveOrUpdateParets(Parets parets){
        Integer flag=0;
        try {
            if(EmptyUtils.isEmpty(parets.getId())){
                flag=paretsService.qdtxAddParets(parets);
            }else{
                flag=paretsService.qdtxModifyParets(parets);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag>=1?ResultUtils.returnSuccess():ResultUtils.returnFail();
    }

    @RequestMapping("/getParetsById")
    @ResponseBody
    public Result getParetsById(Long id){
        Result result= ResultUtils.returnSuccess();
        Parets parets=null;
        try {
            parets=paretsService.getParetsById(id);
            result.setData(parets);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/batchRemoveParets")
    @ResponseBody
    public Result batchRemoveParets(String ids){
        Integer flag=0;
        try {
            flag=paretsService.qdtxBatchDeleteParets(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtils.returnSuccess();
    }

    @RequestMapping("/removeParetsById")
    @ResponseBody
    public Result removeParetsById(Long id){
        Integer flag=0;
        try {
            flag=paretsService.qdtxDeleteParetsById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtils.returnSuccess();
    }
}
